package com.yps.springbootconfigfile.task;

import com.yps.springbootconfigfile.pojo.EmailProperties;
import com.yps.springbootconfigfile.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class sendEmailTask {

    @Autowired
    private EmailProperties emailProperties;

    @Value("${email.to}")
    private String to;
    @Value("${email.title}")
    private String title;
    private String content;

    //每分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void sendEmail() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));
        content = "现在时间是"+sdf.format(date)+"你小子还不赶紧去学习！！！";
        MailUtil.sendMail(emailProperties, to, title, content);
    }

    @Scheduled(cron = "0 0/1 * * * ? ")
    public void time() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));
    }
}
